/*
 * JFrm_ServerRun.java
 *
 * Created on 29 mars 2008, 11:19
 */

package SERVER;

import USECASE.UC_Factory;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author  olivierm
 */
public class JFrm_ServerRun extends javax.swing.JFrame {

    private String url;
    private DefaultListModel _modeleServerStatus = new DefaultListModel();
    
    /** Creates new form JFrm_ServerRun */
    public JFrm_ServerRun() {
        super();
        initComponents();
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtxt_ServerName = new javax.swing.JTextField();
        jtxt_ServerPort = new javax.swing.JTextField();
        jtxt_ServerDirectory = new javax.swing.JTextField();
        jbtn_ServerStop = new javax.swing.JButton();
        jbtn_ServerStart = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jbtn_ServerRefresh = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jbtn_ServerClear = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlist_ServerStatus = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Connection"));

        jLabel1.setText("Server Name:");

        jLabel2.setText("Server Port:");

        jLabel3.setText("Server Directory:");

        jtxt_ServerName.setText("DEVWD-OMA1");

        jtxt_ServerPort.setText("1099");

        jtxt_ServerDirectory.setText("Factory");

        jbtn_ServerStop.setText("Stop");
        jbtn_ServerStop.setEnabled(false);
        jbtn_ServerStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_ServerStopActionPerformed(evt);
            }
        });

        jbtn_ServerStart.setText("Start");
        jbtn_ServerStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_ServerStartActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jbtn_ServerStart)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtn_ServerStop))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addGap(4, 4, 4)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtxt_ServerDirectory)
                            .addComponent(jtxt_ServerPort)
                            .addComponent(jtxt_ServerName, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(252, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxt_ServerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxt_ServerPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtxt_ServerDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtn_ServerStop)
                    .addComponent(jbtn_ServerStart))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jToolBar1.setRollover(true);

        jbtn_ServerRefresh.setText("Refresh");
        jbtn_ServerRefresh.setFocusable(false);
        jbtn_ServerRefresh.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtn_ServerRefresh.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jbtn_ServerRefresh);
        jToolBar1.add(jSeparator1);

        jbtn_ServerClear.setText("Clear");
        jbtn_ServerClear.setFocusable(false);
        jbtn_ServerClear.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtn_ServerClear.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtn_ServerClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_ServerClearActionPerformed(evt);
            }
        });
        jToolBar1.add(jbtn_ServerClear);

        jlist_ServerStatus.setModel(_modeleServerStatus);
        jScrollPane1.setViewportView(jlist_ServerStatus);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Server Status", jPanel2);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 561, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 223, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Connections Status", jPanel3);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 561, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 223, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Objects Status", jPanel4);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Options");
        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtn_ServerStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_ServerStartActionPerformed
        
       if (System.getSecurityManager() == null) {
                    System.setProperty("java.security.policy", "c:\\NetBeans\\java.policy"); 
                    System.setSecurityManager(new RMISecurityManager());
                    this._modeleServerStatus.addElement("java.policy chargé ==> Ok");
       }
        
        try {


            this.url = "rmi://" + jtxt_ServerName.getText() + ":" + jtxt_ServerPort.getText() + "/" + jtxt_ServerDirectory.getText();

            LocateRegistry.createRegistry(Integer.parseInt(jtxt_ServerPort.getText()));
            this._modeleServerStatus.addElement("RMI Registry server creation: OK");
            UC_Factory factory = new UC_Factory();
            this._modeleServerStatus.addElement("UC_Factory instancied: OK");
            Naming.rebind(this.url, factory);
            jbtn_ServerStart.setEnabled(false);
            jbtn_ServerStop.setEnabled(true);
        } catch (RemoteException ex) {
            this._modeleServerStatus.addElement("RMI Registry server creation: FAILED");
            Logger.getLogger(JFrm_ServerRun.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            this._modeleServerStatus.addElement("RMI Binding: FAILED");
            Logger.getLogger(JFrm_ServerRun.class.getName()).log(Level.SEVERE, null, ex);
        }

        
    }//GEN-LAST:event_jbtn_ServerStartActionPerformed

    private void jbtn_ServerStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_ServerStopActionPerformed
        
    }//GEN-LAST:event_jbtn_ServerStopActionPerformed

    private void jbtn_ServerClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_ServerClearActionPerformed
        if (JOptionPane.showConfirmDialog(null, "Etes vous sure de vouloir supprimer le loggin?","Question",JOptionPane.YES_NO_OPTION ) == 0)
            this._modeleServerStatus.removeAllElements();
    }//GEN-LAST:event_jbtn_ServerClearActionPerformed
      
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrm_ServerRun().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton jbtn_ServerClear;
    private javax.swing.JButton jbtn_ServerRefresh;
    private javax.swing.JButton jbtn_ServerStart;
    private javax.swing.JButton jbtn_ServerStop;
    private javax.swing.JList jlist_ServerStatus;
    private javax.swing.JTextField jtxt_ServerDirectory;
    private javax.swing.JTextField jtxt_ServerName;
    private javax.swing.JTextField jtxt_ServerPort;
    // End of variables declaration//GEN-END:variables
    
}
